package com.wu.mvp.app.base;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;

import com.wu.mvp.app.bijection.Presenter;


public class BasePresenter<T> extends Presenter<T> {

    /**
     * 获取Activity
     */
    public Activity getActivity() {
        Activity activity;
        if (getView() instanceof Activity)
            activity = (Activity) getView();
        else if (getView() instanceof Fragment)
            activity = ((Fragment) getView()).getActivity();
        else
            throw new RuntimeException("No View Found" + (getView() == null ? "null" : getView().getClass().getName()));
        return activity;
    }

    public void startActivity(Class<? extends Activity> clazz) {
        Intent intent = new Intent(getActivity(), clazz);
        intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        getActivity().startActivity(intent);
    }

    public void startActivity(Intent intent) {
        intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        getActivity().startActivity(intent);
    }

    public void startActivityForResult(Class<? extends Activity> clazz, int requestCode) {
        Intent intent = new Intent(getActivity(), clazz);
        intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        getActivity().startActivityForResult(intent, requestCode);
    }

    public void startActivityForResult(Intent intent, int requestCode) {
        intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        getActivity().startActivityForResult(intent, requestCode);
    }
}

